package com.ruoyi.aiexperiments.controller;

import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.aiexperiments.domain.AiEthicsReview;
import com.ruoyi.aiexperiments.service.IAiEthicsReviewService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.RemoteProjectResearchService;
import com.ruoyi.system.api.domain.ProjectResearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.aiexperiments.domain.AiExperiment;
import com.ruoyi.aiexperiments.service.IAiExperimentService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.utils.SecurityUtils;

/**
 * 人工智能实验基本信息Controller
 * 
 * @author czj
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/experiment")
public class AiExperimentController extends BaseController
{
    @Autowired
    private IAiExperimentService aiExperimentService;


    @Autowired
    private IAiEthicsReviewService aiEthicsReviewService;

    @Autowired
    private RemoteProjectResearchService remoteProjectResearchService;

    /**
     * 查询人工智能实验基本信息列表
     */
    //@RequiresPermissions("aiexperiments:Experiment:list")
    @GetMapping("/list")
    public TableDataInfo list(AiExperiment aiExperiment)
    {
        aiExperiment.setDeleteFlag(0);
        startPage();
        List<AiExperiment> list = aiExperimentService.selectAiExperimentList(aiExperiment);
        for (AiExperiment aiExperiment1 :list){
            R<ProjectResearch> researchR = remoteProjectResearchService.getProjectInfo(aiExperiment1.getProjectId());
            ProjectResearch research=researchR.getData();
            aiExperiment1.setProjectName(research.getProjectName());
        }
        return getDataTable(list);
    }
    //@RequiresPermissions("aiexperiments:Experiment:list")
    @PostMapping("/list1")
    public List<AiExperiment> list1(AiExperiment aiExperiment)
    {
        aiExperiment.setDeleteFlag(0);
        startPage();
        List<AiExperiment> list = aiExperimentService.selectAiExperimentList(aiExperiment);
        for (AiExperiment aiExperiment1 : list) {
            System.out.println(aiExperiment1.toString());
        }
        return list;
    }

    /**
     * 导出人工智能实验基本信息列表
     */
    @RequiresPermissions("aiexperiments:Experiment:export")
    @Log(title = "人工智能实验基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AiExperiment aiExperiment)
    {
        List<AiExperiment> list = aiExperimentService.selectAiExperimentList(aiExperiment);
        ExcelUtil<AiExperiment> util = new ExcelUtil<AiExperiment>(AiExperiment.class);
        util.exportExcel(response, list, "人工智能实验基本信息数据");
    }

    /**
     * 获取人工智能实验基本信息详细信息
     */
    @RequiresPermissions("aiexperiments:Experiment:query")
    @GetMapping(value = "/{experimentId}")
    public AjaxResult getInfo(@PathVariable("experimentId") Long experimentId)
    {
        AiExperiment aiExperiment = aiExperimentService.selectAiExperimentByExperimentId(experimentId);
        R<ProjectResearch> researchR = remoteProjectResearchService.getProjectInfo(aiExperiment.getProjectId());
        ProjectResearch research=researchR.getData();
        aiExperiment.setProjectName(research.getProjectName());
        return success(aiExperiment);
    }

    /**
     * 新增人工智能实验基本信息
     */
    //@RequiresPermissions("aiexperiments:Experiment:add")
    @Log(title = "人工智能实验基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AiExperiment aiExperiment)
    {
        aiExperiment.setCreatedBy(SecurityUtils.getUsername());
        aiExperiment.setCreatedAt(new Date());
        return toAjax(aiExperimentService.insertAiExperiment(aiExperiment));
    }

    /**
     * 修改人工智能实验基本信息
     */
    @RequiresPermissions("aiexperiments:Experiment:edit")
    @Log(title = "人工智能实验基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody AiExperiment aiExperiment)
    {
        aiExperiment.setUpdatedBy(SecurityUtils.getUsername());
        aiExperiment.setUpdatedAt(new Date());
        aiExperiment.setStatus("1");
        AiEthicsReview aiEthicsReview = new AiEthicsReview();
        aiEthicsReview.setExperimentId(aiExperiment.getExperimentId());
        aiEthicsReviewService.insertAiEthicsReview(aiEthicsReview);
        return toAjax(aiExperimentService.updateAiExperiment(aiExperiment));
    }

    /**
     * 删除人工智能实验基本信息
     */
    @RequiresPermissions("aiexperiments:Experiment:remove")
    @Log(title = "人工智能实验基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{experimentIds}")
    public AjaxResult remove(@PathVariable Long[] experimentIds)
    {
        for (Long experimentId : experimentIds) {
            AiExperiment aiExperiment = aiExperimentService.selectAiExperimentByExperimentId(experimentId);
            if (aiExperiment == null) {
                return error("实验信息不存在");
            }

            aiExperiment.setDeletedBy(SecurityUtils.getUsername());
            aiExperiment.setDeletedAt(new Date());
            aiExperiment.setDeleteFlag(1);
            aiExperimentService.updateAiExperiment(aiExperiment);
        }
        return toAjax(true);
    }
}
